﻿/// <reference Path="../jquery-1.3.2-vsdoc.js" />
$(function () {
    // 购买记录
    var loadSales = function () {
        $.getJSON(window.pv_saleHisotry_url, { rnd: Math.random(), productId: window.pv_productId }, function (result) {
            if (result.length > 0) {
                $("#saleHistory").empty()
                var oTable = $("<table><tr class='fontBold'><td>买家昵称</td><td>购买数量</td><td>出价</td><td>购买时间</td></tr></table>")
                $.each(result, function (i, n) {
                    var otr = $("<tr></tr>")
                    otr.append("<td>" + n.NickName + "</td>");
                    otr.append("<td class='colorOrange'>" + n.Uid + "</td>");
                    otr.append("<td>" + n.Uname + "</td>");
                    otr.append("<td>" + new Date(parseInt(n.RegTime.substring(6, n.RegTime.length - 2))).toLocaleString() + "</td>");
                    oTable.append(otr)
                });
                $("#saleHistory").append(oTable)
            }
        });
    }
    // 评价和咨询
    var loadConsult = function (object, url) {
        $.post(url, { productId: window.pv_productId }, function (result) {
            $(object).html(result);
            $(object).find("#btnConsult").click(function () {
                $.post(window.pv_productConsult_search_url, {
                    productId: window.pv_productId,
                    key: $("#txtConsultKey").val()
                }, function (result) {
                    $("#div_consult_list").html(result);
                });
            });
        });
    };
    window.backConsult = function () {
        loadConsult("#consults", window.pv_productConsult_list_url);
    };
    $.history.init(function (hash) {
        $("#details").tabs("select", parseInt(hash == "" ? "0" : hash));
    });
    $("#details").tabs({
        select: function (event, ui) {
            switch (ui.index) {
                case 4:
                    loadSales();
                    break;
                case 5:
                    loadConsult("#comments", window.pv_productComment_list_url);
                    break;
                case 6:
                    loadConsult("#consults", window.pv_productConsult_list_url);
                    break;
            }
            $.history.load(ui.index);
        }
    });
    //可选属性选择
    $(".sameProduct a").click(function () { $(this).parent().find("a").removeClass("cur"); this.className = "cur"; });
    //var imagehtml = $("#divAlbum").html();
    $(".shareProduct .b").click(function () {
        $("#divMaxShow").ViewImage({ space: 64, loadimageurl: window.pv_loadimage_url });        
    }); 
    $(".shareProduct .s").click(function () { alert("分享给朋友功能完善中..."); }); // todo
    // 加入到购物车
    $("#addToCar").click(function () {
        var isValid = true;
        var optional = [];
        $(".sameProduct").each(function (i, n) {
            if ($(n).find("dd a.cur").length != 1) {
                alert("请选择:" + $(n).find("dt span").text());
                isValid = false;
                return isValid;
            };
            optional[i] = $(n).find("dd a.cur").attr("accesskey");
        });
        if (!isValid)
            return false;
        $.post(window.pv_addToCar_url, { ProductId: window.pv_productId, count: 1, optional: optional, rnd: Math.random() }, function (result) {
            result = $(result);
            window.CloseDialog = function () { result.dialog("close") }
            result.dialog({ width: 500, modal: true, closeText: "×", close: function () { $(this).dialog("destroy") } });
            ReLoadMiNiCar();
        });
    });
    $("#addToFavorite").click(function () {
        $.post(window.addToFavorite_url, { productId: window.pv_productId, rnd: Math.random() }, function (result) {
            result = $(result);
            window.CloseDialog = function () { result.dialog("close") }
            result.dialog({ width: 500, modal: true, closeText: "×", close: function () { $(this).dialog("destroy") } });
        });
    });
    // 图片
    $("#divAlbum").ScrollImage({
        image: { space: 54, shownum: 5 },
        onimageclick: function () {
            $("#cover img").attr("src", $(this).attr("src"));
            $("#cover a").attr("href", $(this).attr("src").replace(/\/s_(.*)/ig, "/$1"));
        }
    });
    $("#cover a").jqzoom({ zoomWidth: 448, zoomHeight: 300, title: false });
    // 销售排行
    var liClone = null, loadHtml = $("#divProductRank").html();
    var ids = [window.pv_classifyId, window.pv_brandId, window.pv_productId];
    var urls = [window.pv_productClassifyRank_url, window.pv_productBrandRank_url, window.pv_productPriceRank_url];
    $(".ulTabTitle li").each(function (i, n) {
        var li = $(this), p = li.parent(), os = li.offset();
        li.click(function () {
            if (liClone == null) { liClone = li.clone().attr("class", "curr"); p.append($(liClone)); }
            $(liClone).text(li.text()).css({ top: os.top, left: os.left });

            $("#divProductRank").html(loadHtml);
            $.post(urls[i], { id: ids[i] }, function (html) {
                var liHtml = $("#divProductRank").html(html);
                var css = { width: "auto", height: "18px", marginLeft: "12px", padding: "0px" };
                liHtml.find(".i").hide(); liHtml.find(".d").css(css);
                liHtml.find("li").hover(function () {
                    var silbings = $(this).siblings("li");
                    silbings.removeClass("out");
                    silbings.find(".i").hide();
                    silbings.find(".d").css(css).find("strong").hide();
                    $(this).find(".i").show();
                    $(this).find(".d").removeAttr("style").find("strong").show();
                    $(this).addClass("out");
                });
                liHtml.find("li").eq(0).hover();
            });
        });
    }).eq(0).click();
    // 历史记录
    var divHP = $("#divHistoryProduct");
    $.post(window.pv_productHistory_url, null, function (html) {
        loadHtml = divHP.html(); divHP.html(html);
    });
    $("#emHistoryProduct").click(function () {
        divHP.html(loadHtml);
        $.post(window.pv_productHistory_clear_url, null, function (html) {
            divHP.html(html);
        });
    });
});